﻿using HomagGroup.Base.UI.Controls;
using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Windows;
using LocalText = Proxy.Properties.Resources;
using HomagChina.CCHS.Views;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace HomagChina.CCHS.ViewModels
{
    public class BoardEditingViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private SearchInfoHandle Handle_Search;
        private InsertInfoHandle Handle_Insert;
        private ModifyInfoHandle Handle_modify;
        private CommonHandle commonHandle;

        #endregion Handle

        #region 属性

        private string _Title;
        private string _OriginalBoardcode;
        private string _Boardcode;
        private string _Material;
        private float _Length;
        private float _Width;
        private float _Thickness;
        private int _Type;
        private int _Minstock;
        private int _Shelflife;
        private string _Description;
        private string _SaveAsVisible;
        private string _EditVisible;
        private string _SaveVisible;
        private string _DropDownList_Material_Visible;
        private string _InputField_Material_Visible;
        private bool _IsReadOnly;
        private bool _DataChanged;

        public string Title
        {
            get => _Title;
            set { _Title = value; OnPropertyChanged(); }
        }

        public string OriginalBoardcode
        {
            get => _OriginalBoardcode;
            set { _OriginalBoardcode = value; }
        }

        public string Boardcode
        {
            get => _Boardcode;
            set { _Boardcode = value; OnPropertyChanged(); }
        }

        public float Length
        {
            get => _Length;
            set { _Length = value; OnPropertyChanged(); }
        }

        public float Width
        {
            get => _Width;
            set { _Width = value; OnPropertyChanged(); }
        }

        public float Thickness
        {
            get => _Thickness;
            set { _Thickness = value; OnPropertyChanged(); }
        }

        public string Material
        {
            get => _Material;
            set { _Material = value; OnPropertyChanged(); }
        }

        public int Type
        {
            get => _Type;
            set { _Type = value; OnPropertyChanged(); }
        }

        public int Minstock
        {
            get => _Minstock;
            set { _Minstock = value; OnPropertyChanged(); }
        }

        public int Shelflife
        {
            get => _Shelflife;
            set { _Shelflife = value; OnPropertyChanged(); }
        }
        public string Description
        {
            get => _Description;
            set { _Description = value; OnPropertyChanged(); }
        }

        public bool DataChanged
        {
            get => _DataChanged;
            set { _DataChanged = value; }
        }

        public string SaveAsVisible
        {
            get => _SaveAsVisible;
            set { _SaveAsVisible = value; OnPropertyChanged(); }
        }

        public string EditVisible
        {
            get => _EditVisible;
            set { _EditVisible = value; OnPropertyChanged(); }
        }

        public string SaveVisible
        {
            get => _SaveVisible;
            set { _SaveVisible = value; OnPropertyChanged(); }
        }

        public string DropDownList_Material_Visible
        {
            get => _DropDownList_Material_Visible;
            set { _DropDownList_Material_Visible = value; OnPropertyChanged(); }
        }

        public string InputField_Material_Visible
        {
            get => _InputField_Material_Visible;
            set { _InputField_Material_Visible = value; OnPropertyChanged(); }
        }
        public bool IsReadOnly
        {
            get => _IsReadOnly;
            set { _IsReadOnly = value; OnPropertyChanged(); }
        }

        /// <summary>
        /// Dropdown list
        /// </summary>
        public object SelectedValue;
        /// <summary>
        /// Dropdown list
        /// </summary>
        public object SelectedValue_Type;

        public RelayCommand ApplyCommand { get; }

        public RelayCommand ModifyCommand { get; }

        public RelayCommand LoadedCommand { get; }

        #endregion 属性

        #region components

        public DropDownList DropDownList_Material { get; set; }
        public DropDownList DropDownList_Type { get; set; }



        private ObservableCollection<BoardInfoModel> _List_BoardInfoModel;

        public ObservableCollection<BoardInfoModel> List_BoardInfoModel
        {
            get => _List_BoardInfoModel;
            set { _List_BoardInfoModel = value; OnPropertyChanged(); }
        }
        #endregion components

        public BoardEditingViewModel(BoardInfoModel ModifyboardInfoModel, Boolean IsEdit)
        {
            Title = IsEdit ? LocalText.BoardEditing : LocalText.BoardAdding;
            SaveAsVisible = IsEdit ? "Visible" : "Collapsed";
            EditVisible = IsEdit ? "Visible" : "Collapsed";
            SaveVisible = IsEdit ? "Collapsed" : "Visible";
            DropDownList_Material_Visible = IsEdit ? "Visible" : "Collapsed";
            InputField_Material_Visible = IsEdit ? "Collapsed" : "Visible";
            ApplyCommand = new RelayCommand(Apply);
            ModifyCommand = new RelayCommand(Modify);
            LoadedCommand = new RelayCommand(ViewLoadedCommand);
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_modify = new ModifyInfoHandle();
            commonHandle = new CommonHandle();
            LoadDropDownList();
            if (ModifyboardInfoModel != null)
            {
                Boardcode = ModifyboardInfoModel.Boardcode;
                Length = (float)ModifyboardInfoModel.Length;
                Width = (float)ModifyboardInfoModel.Width;
                Thickness = (float)ModifyboardInfoModel.Thickness;
                Material = ModifyboardInfoModel.Material;
                DropDownList_Material.SelectedValue = ModifyboardInfoModel.Material;
                Type = int.Parse(ModifyboardInfoModel.Boardtype.Split('-')[0]);
                DropDownList_Type.SelectedValue = ModifyboardInfoModel.Boardtype;
                Minstock = (int)ModifyboardInfoModel.Minstock;
                Description = ModifyboardInfoModel.Description;
                Shelflife = (int)ModifyboardInfoModel.Shelflife;
            }
        }


        public void ViewLoadedCommand(object InputFieldInfo)
        {
            OriginalBoardcode = ((InputField)InputFieldInfo).Text;
        }

        #region DropDownList

        /// <summary>
        /// 加载DropDownList内容
        /// Load the DropDownList
        /// </summary>
        public void LoadDropDownList()
        {
            DropDownList_Material = new DropDownList();
            var materialList = Handle_Search.GetMaterialList();
            materialList.Add("Add new material");
            DropDownList_Material.ItemsSource = materialList;
            SelectedValue = DropDownList_Material.Items.CurrentItem;
            DropDownList_Material.SelectedIndex = 0;
            DropDownList_Material.SelectionChanged += DropDownList_SelectionChanged;

            DropDownList_Type = new DropDownList();
            DropDownList_Type.ItemsSource = (new List<string>() { { "0-垫板" }, { "1-原材料" }, { "2-余料" } });
            Type = int.Parse(DropDownList_Type.Items.CurrentItem.ToString().Split('-')[0]);
            DropDownList_Type.SelectedIndex = 0;
            DropDownList_Type.SelectionChanged += DropDownListType_SelectionChanged;
        }

        private void DropDownList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((DropDownList)sender).SelectedItem == null)
            {
                return;
            }
            SelectedValue = ((DropDownList)sender).SelectedItem.ToString();
            if (SelectedValue.Equals("Add new material"))
            {
                BoardEditingViewModel boardEditingViewModel = new BoardEditingViewModel(null, false);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<BoardEditing>(boardEditingViewModel, null);
                DropDownList_Material.ItemsSource = null;
                var materialList = Handle_Search.GetMaterialList();
                materialList.Add("Add new material");
                DropDownList_Material.ItemsSource = materialList;
            }
        }
        private void DropDownListType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Type = int.Parse(((DropDownList)sender).SelectedItem.ToString().Split('-')[0]);
        }

        #endregion DropDownList

        private void Apply()
        {
            CCHS_BoardlibraryModel model = new CCHS_BoardlibraryModel()
            {
                Boardcode = Boardcode,
                Length = Length,
                Width = Width,
                Thickness = Thickness,
                Material = Material,
                Boardtype = Type,
                Minstock = Minstock,
                Shelflife = Shelflife,
                Description = Description,
                CreationDate = DateTime.Now
            };

            var Returnconstruct = Handle_Insert.InsertBoardInfo(model);
            switch (Returnconstruct.Code)
            {
                case -99:
                    HomagGroup.Base.UI.Windows.MessageBox.Show(Returnconstruct.Msg);
                    break;
                case -1:
                    HomagGroup.Base.UI.Windows.MessageBox.Show(LocalText.Sorry_information_addition_failed_please_try_again_or_contact_administrator);
                    break;
                case 1:
                    HomagGroup.Base.UI.Windows.MessageBox.Show(LocalText.Congratulations_information_added_successfully);
                    commonHandle.CloseDiaLog(Title);
                    break;
                case 2:
                    BoardReplaceViewModel boardreplaceViewModel = new BoardReplaceViewModel(model);
                    UIAdjustments.CurrentThreadDialogManager.ShowDialog<BoardReplace>(boardreplaceViewModel, null);
                    break;
                default:
                    break;
            }
        }

        private void Modify()
        {
            if (!Boardcode.Equals(OriginalBoardcode))
            {
                HomagGroup.Base.UI.Windows.MessageBox.Show(LocalText.Original_boardcode_changed_please_use__Save_as__to_save_the_new_data_);
                return;
            }
            CCHS_BoardlibraryModel model = new CCHS_BoardlibraryModel()
            {
                Boardcode = Boardcode,
                Length = Length,
                Width = Width,
                Thickness = Thickness,
                Material = SelectedValue.ToString(),
                Boardtype = Type,
                Minstock = Minstock,
                Shelflife = Shelflife,
                Description = Description,
            };
            var msg = Handle_modify.ModifyBoardlibraryInfo(model) ? LocalText.Congratulations_information_added_successfully
                : LocalText.Sorry_information_addition_failed_please_try_again_or_contact_administrator;
            HomagGroup.Base.UI.Windows.MessageBox.Show(msg.ToString());
            commonHandle.CloseDiaLog(Title);
        }
    }
}